<template>
  <el-dialog
    width="1000px"
    :title="$t('追加')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel"
  >
    <el-form :model="queryParams" ref="queryForm" size="small" inline label-width="90px">
      <el-form-item :label="$t('sku编号')" prop="skuNo">
        <el-input
          v-model="queryParams.skuNo"
          :placeholder="$t('请输入')"
          clearable
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item :label="$t('供应商名称')" prop="distrName">
        <el-input
          v-model="queryParams.distrName"
          :placeholder="$t('请输入')"
          clearable
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item :label="$t('中文名')" prop="skuNameCn">
        <el-input
          v-model="queryParams.skuNameCn"
          :placeholder="$t('请输入')"
          clearable
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item :label="$t('日文名')" prop="skuNameJp">
        <el-input
          v-model="queryParams.skuNameJp"
          :placeholder="$t('请输入')"
          clearable
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
          {{ $t('搜索') }}
        </el-button>
      </el-form-item>
    </el-form>
    <el-table :height="calTableHeight() - 200" :data="tableData" ref="multipleTable">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- sku -->
      <el-table-column align="center" show-overflow-tooltip prop="skuNo" :label="$t('sku编号')">
        <template slot-scope="scope">
          <div @click="handleEdits(scope.row)" style="cursor: pointer; color: #1890ff">
            {{ scope.row.skuNo }}
          </div>
        </template>
      </el-table-column>
      <!-- 图片 -->
      <el-table-column prop="imgUrl" :label="$t('图片')" align="center" width="100">
        <template slot-scope="scope">
          <el-image
            fit="contain"
            :key="Math.random()"
            v-hover="scope.row.imgUrl"
            style="width: 50px; height: 50px"
            :src="scope.row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="skuNameCn" width="200" :label="$t('中文名')" align="center" />
      <el-table-column show-overflow-tooltip prop="skuNameJp" width="200" :label="$t('日文名')" align="center" />

      <el-table-column prop="combinationNum" :label="$t('组合数量')" align="center">
        <template slot-scope="scope">
          <el-input-number
            :precision="0"
            style="width: 100px"
            v-model="scope.row.combinationNum"
            controls-position="right"
            :min="1"
          />
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('操作')" width="100">
        <template slot-scope="{ row }">
          <el-button type="text" @click="handleSelect(row)">
            {{ $t('选择') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">{{ $t('关闭') }}</el-button>
    </div>
    <!-- sku弹窗 -->
    <Skucontmoduler @relaod="getList" ref="Skucontmoduler"></Skucontmoduler>
  </el-dialog>
</template>

<script>
import { getCombinationSkuListById } from '@/api/distribution/sku/assemblyList'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: {},
    },
    /**
     * type
     */
    type: {
      type: String,
      default: 'add',
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      tableData: [],
      form: {},
      // 表单校验
      rules: {},
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        // this.resetForm()
        this.resetQuery()
        this.getList()
      }
    },
  },
  methods: {
    handleEdits(row) {
      this.$refs.Skucontmoduler.setSkuInfo(row)
      this.$refs.Skucontmoduler.checkNumber('packingInformation')
    },
    handleSelect(row) {
      if (!row.combinationNum) {
        this.$message.warning(this.$t('请输入') + '!')
        return
      }
      this.$emit('select', JSON.parse(JSON.stringify(row)))
      this.getList()

      // this.cancel()
    },
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = ''))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
    },
    getList() {
      getCombinationSkuListById(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.total = res.total
      })
    },

    resetForm() {
      Object.keys(this.form).forEach((key) => (this.form[key] = ''))
    },

    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
